---
--- http请求工具
--- Created by E.T.
--- DateTime: 2018/10/25 14:30
---


local http = require "socket.http"
local ltn12 = require "ltn12"
local cjson = require"cjson"
local _M = {}
function http.get(u)
    local t = {}

    local r, c, h = http.request {
        url = u,
        method = "GET",
        headers = {
            ["Content-Type"] = "application/json";
            ["Accept"] = "application/json";
        },
        source = ltn12.source.string(request_body),
        sink = ltn12.sink.table(t) }
    return r, c, h, table.concat(t)
end

-- 发送http.get请求，返回响应结果
function _M.web_get(url)
    r, c, h, body = http.get(url)
    return body, c
end

--kong特有
--返回给客户端
function _M.response(code, content)
    local res_table={}
    local body_table={}
    res_table["code"] = code
    res_table["message"] = content
    res_table["responseBody"] = body_table
    body_table["code"] = -1
    body_table["data"] = {}
    body_table["message"] = content
    local jsonStr = cjson.encode(res_table);
    return kong.response.exit(code, jsonStr, {
        ["Content-Type"] = "application/json",
        ["WWW-Authenticate"] = "Basic"
    })
end

return _M


